package cn.xjt.controller;

import cn.xjt.Utils.JsonUtils;
import cn.xjt.pojo.User;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.FieldPosition;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * 内容：
 *
 * @author
 * @date 2020/9/22-14:17
 */

@Controller     //会走视图解析器
//@RestController //不会走视图解析器，会返回字符串
public class UserController {

	@RequestMapping(value = "/j1")
	@ResponseBody       //它就不会走视图解析器，会直接返回一个字符串，在类注解为Controller的时候如果要返回字符串可以使用
	public String test() throws JsonProcessingException {

		//jackson   ,  ObjectMapper

		ObjectMapper mapper = new ObjectMapper();

		User user = new User("徐江涛",18,"男");

		String str = mapper.writeValueAsString(user);

		return str;
	}
	@RequestMapping("/j2")
	@ResponseBody       //不会走视图解析器
	public String test2() throws JsonProcessingException {

		List<User> users = new ArrayList<User>();

		User user = new User("徐江涛1号",18,"男");
		User user1 = new User("徐江涛2号",18,"男");
		User user2 = new User("徐江涛3号",18,"男");
		User user3 = new User("徐江涛4号",18,"男");
		User user4 = new User("徐江涛5号",18,"男");

		users.add(user);
		users.add(user1);
		users.add(user2);
		users.add(user3);
		users.add(user4);

		String str = JsonUtils.getJson(users);

		return str;
	}

	@RequestMapping("/j3")
	@ResponseBody
	/**
	 * 通过java基础将时间返回给前端
	 */
	public String test3() throws JsonProcessingException {
		Date date = new Date();

//		自定义日期格式
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		String time = sd.format(date);
		ObjectMapper mapper = new ObjectMapper();

		String str = mapper.writeValueAsString(date);

		return time;
	}

	@RequestMapping("/j4")
	@ResponseBody
	/**
	 * 使用OBjectMapper得到时间格式，返回
	 */
	public String test4() throws JsonProcessingException {

		ObjectMapper mapper = new ObjectMapper();

		mapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS,false);

		//		自定义日期格式
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		mapper.setDateFormat(sd);

		Date date = new Date();

		return mapper.writeValueAsString(date);
	}

	@RequestMapping("/jj")
	@ResponseBody       //不会经过视图解析器，直接返回字符串
	public String test5(){

		Date date = new Date();

		String sdf="yyyy-MM-dd HH:mm:ss";

		String time = JsonUtils.getJson(date, sdf);

		return time;
	}

	@RequestMapping("/aj")
	@ResponseBody       //不会经过视图解析器，直接返回字符串
	public String test6(){

		List<User> users = new ArrayList<User>();

		User user = new User("徐江涛1号",18,"男");
		User user1 = new User("徐江涛2号",18,"男");
		User user2 = new User("徐江涛3号",18,"男");
		User user3 = new User("徐江涛4号",18,"男");
		User user4 = new User("徐江涛5号",18,"男");

		users.add(user);
		users.add(user1);
		users.add(user2);
		users.add(user3);
		users.add(user4);

		String string = JSON.toJSONString(users);

		return string;
	}
}
